/*
*
* File Name          :  DSP.c
* Description        :  DSP Functions
*/


#include <stdint.h>
#include "DSP.h"
#ifdef _STM32x_
#include "circle_api.h"
#include "ClockCounter.h"
#endif

#ifdef DSP_CMSIS

arm_status _DSP_fft_status;
arm_cfft_radix4_instance_q15 _DSP_fft_instance;
q15_t _DSP_FFT_Sample[2*DSP_FFT_SAMPLE_LENGTH];

int16_t _DSP_length;
void DSP_Init(int16_t length) {
  _DSP_length = length;
  _DSP_fft_status = arm_cfft_radix4_init_q15(&_DSP_fft_instance, DSP_FFT_SAMPLE_LENGTH, 0, 1);
}

void DSP_Fft(FPComplex *output, FPComplex *input) { 
    arm_cfft_radix4_q15(&_DSP_fft_instance, (q15_t *) input);
}

void DSP_Ifft(FPComplex *output, FPComplex *input) { 
    arm_cfft_radix4_q15(&_DSP_fft_instance, (q15_t *) input);
}

#elif DSP_KISS

#include "kiss_fft.h"

kiss_fft_cfg _DSP_fft_instance;
kiss_fft_cfg _DSP_ifft_instance;
int16_t _DSP_length;

void DSP_Init(int16_t length) {
  _DSP_length = length;
  _DSP_fft_instance = kiss_fft_alloc(_DSP_length, 0, 0, 0);
  _DSP_ifft_instance = kiss_fft_alloc(_DSP_length, 1, 0, 0);
}

void DSP_Fft(FPComplex *output, FPComplex *input) {
  kiss_fft(_DSP_fft_instance, (const kiss_fft_cpx *)input, (kiss_fft_cpx *) output);
}

void DSP_Ifft(FPComplex *output, FPComplex *input) {
  kiss_fft(_DSP_ifft_instance, (const kiss_fft_cpx *)input, (kiss_fft_cpx *) output);
}

#else

int16_t _DSP_length;
void DSP_Init(int16_t length) {
  _DSP_length = length;
}

void DSP_Fft(FPComplex *output, FPComplex *input) { 
    fftR4(output, input, _DSP_length);
}

void DSP_Ifft(FPComplex *output, FPComplex *input) {
    ifftR4(output, input, _DSP_length);
}

#endif
